/**
 * 
 */
package com.appwx.sdk.actionbar;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

import com.appwx.sdk.R;
import com.appwx.sdk.animation.BackAnimimation;

/**
 * @author xiajun
 * 
 */
public class ParallaxScrollViewB extends ScrollView {

	private ArrayList<onOverScrollByListener> mOnOverScrollByList = new ArrayList<onOverScrollByListener>();
	private ArrayList<onTouchEventListener> mOnTouchEventList = new ArrayList<onTouchEventListener>();
	private View mParallaxView;
	private int mDrawableMaxHeight = -1;
	private int mImageViewHeight = -1;
	public final static double NO_ZOOM = 1;
	private double mZoomRatio = 1;

	public ParallaxScrollViewB(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView(context, attrs, defStyle);
	}

	public ParallaxScrollViewB(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context, attrs, 0);
	}

	public ParallaxScrollViewB(Context context) {
		super(context);
		initView(context, null, 0);
	}

	private void initView(Context context, AttributeSet attrs, int defStyle) {
		if (isInEditMode())
			return;

		if (attrs != null) {
			TypedArray values = context.obtainStyledAttributes(attrs,
					R.styleable.ParallaxScrollViewB);

			mZoomRatio = values.getFloat(
					R.styleable.ParallaxScrollViewB_zoomRatio,
					(float) mZoomRatio);

			values.recycle();
		}

		// 在设置View边界时,使用线程防止UI线程阻塞
		post(new Runnable() {

			@Override
			public void run() {

				setViewsBounds(mZoomRatio);

			}
		});

	}

	@SuppressLint("NewApi")
	public void setViewsBounds(double zoomRatio) {
		double imageRatio;
		if (mImageViewHeight == -1) {

			mImageViewHeight = mParallaxView.getHeight();//获取View的Height

			imageRatio = ((double) mParallaxView.getWidth()) / ((double) mParallaxView.getWidth());

			mDrawableMaxHeight = (int) ((mParallaxView.getHeight() / imageRatio) * (zoomRatio > 1 ? zoomRatio: 1));

		}
	}

	/**
	 * 设置要改变视图差效果的View
	 * 
	 * @param view
	 */
	public void setViewToParallax(View parallaxView) {
		mParallaxView = parallaxView;
		addOnScrolledListener(onScroll);
		addOnTouchListener(onTouched);
	}

	private void addOnTouchListener(onTouchEventListener onTouched) {
		mOnTouchEventList.add(onTouched);
	}

	private void addOnScrolledListener(onOverScrollByListener onScroll) {
		mOnOverScrollByList.add(onScroll);
	}

	private onOverScrollByListener onScroll = new onOverScrollByListener() {

		@Override
		public boolean overScrollBy(int deltaX, int deltaY, int scrollX,
				int scrollY, int scrollRangeX, int scrollRangeY,
				int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {


			if (mParallaxView.getHeight() <= mDrawableMaxHeight && isTouchEvent) {

				if (deltaY < 0) {

					if (mParallaxView.getHeight() - deltaY / 2 >= mImageViewHeight) {

						mParallaxView.getLayoutParams().height = mParallaxView
								.getHeight() - deltaY / 2 < mDrawableMaxHeight ? mParallaxView
								.getHeight() - deltaY / 2
								: mDrawableMaxHeight;

						mParallaxView.requestLayout();

					}

				} else {

					if (mParallaxView.getHeight() > mImageViewHeight) {

						mParallaxView.getLayoutParams().height = mParallaxView
								.getHeight() - deltaY > mImageViewHeight ? mParallaxView
								.getHeight() - deltaY
								: mImageViewHeight;

						mParallaxView.requestLayout();

						return true;

					}

				}

			}

			return false;
			
		}
	};

	private onTouchEventListener onTouched = new onTouchEventListener() {

		@Override
		public void onTouchEvent(MotionEvent ev) {

			if (ev.getAction() == MotionEvent.ACTION_UP) {

				if (mImageViewHeight - 1 < mParallaxView.getHeight()) {
					BackAnimimation animation = new BackAnimimation(mParallaxView,mImageViewHeight, false);
					animation.setDuration(200);
					mParallaxView.startAnimation(animation);
				}

			}
		}
	};

	protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
			int scrollY, int scrollRangeX, int scrollRangeY,
			int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
		
		boolean isCollapseAnimation = false;

		for (int i = 0; i < mOnOverScrollByList.size(); i++) {

			isCollapseAnimation = mOnOverScrollByList.get(i).
					overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,
					scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent)|| isCollapseAnimation;

		}
				return isCollapseAnimation ? true : super.overScrollBy(deltaX, deltaY,
						scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX,
						maxOverScrollY, isTouchEvent);
	};

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		
		for (int i = 0; i < mOnTouchEventList.size(); i++) {
			mOnTouchEventList.get(i).onTouchEvent(ev);
		}
		
		return super.onTouchEvent(ev);
	}

	private interface onOverScrollByListener {

		public boolean overScrollBy(int deltaX, int deltaY, int scrollX,
				int scrollY, int scrollRangeX, int scrollRangeY,
				int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent);

	}

	private interface onTouchEventListener {

		public void onTouchEvent(MotionEvent ev);

	}

}
